import React from "react"
import {Route,Redirect} from "react-router-dom"
import Login from './pages/Info/login';
import CaiLing from "./pages/Info/CaiLing"
import Guanzhu from "./pages/Info/guanzhu"
import Dongtai from "./pages/Info/dongtai"
import MyInformation from './pages/Info/MyInformation';
import Fensi from './pages/Info/fensi';
import Personhome from './pages/Info/personhome';
import Page404 from './pages/Info/404';
import Tab_bar from "./Tabbar"
import { propsModel } from './frontend-auth.model'

const routerConfig=[
    {
        path:'/',
        component:Login,
        auth:true
    },
    {
        path:'/cailing',
        component:CaiLing,
        auth:true
    },
    {
        path:'/guanzhu',
        component:Guanzhu,
        auth:true
    },
    {
        path:'/fensi',
        component:Fensi,
        auth:true
    },
    {
        path:'/information',
        component:MyInformation,
        auth:true
    },
    {
        path:'/dongtai',
        component:Dongtai,
        auth:true
    },
    {
        path:'/person',
        component:Personhome,
        auth:true
    },
    {
        path:'/404',
        component:Page404,
        auth:true
    },
    {
        path:'/info',
        component:Tab_bar,
        auth:true
    },
    {
        path:'/cloud',
        component:Tab_bar,
        auth:true
    },
    {
        path:'/my',
        component:Tab_bar,
        auth:true
    },
    {
        path:'/video',
        component:Tab_bar,
        auth:true
    },
    {
        path:'/discover',
        component:Tab_bar,
        auth:true
    },
]

class FrontendAuth extends React.Component<any,propsModel>{
    render(){
        const { location,config } = this.props;
        const { pathname } = location;
        const isLogin = localStorage.getItem('uId')
        
        // 如果该路由不用进行权限校验，登录状态下登陆页除外
        // 因为登陆后，无法跳转到登陆页
        // 这部分代码，是为了在非登陆状态下，访问不需要权限校验的路由
        const targetRouterConfig = config.find((v:any) => v.path === pathname);
        if(targetRouterConfig && !targetRouterConfig.auth && !isLogin){
            const { component } = targetRouterConfig;
            return <Route exact path={pathname} component={component} />
        }

        if(isLogin){
            // 如果是登陆状态，想要跳转到登陆，重定向到主页
            if(pathname === '/login'){
                return <Redirect to='/' />
            }else{
                // 如果路由合法，就跳转到相应的路由
                if(targetRouterConfig){
                    return <Route path={pathname} component={targetRouterConfig.component} />
                }else{
                    // 如果路由不合法，重定向到 404 页面
                    return <Redirect to='/404' />
                }
            }
        }else{
            // 非登陆状态下，当路由合法时且需要权限校验时，跳转到登陆页面，要求登陆
            if(targetRouterConfig && targetRouterConfig.auth){
                return <Redirect to='/login' />
            }else{
                // 非登陆状态下，路由不合法时，重定向至 404
                return <Redirect to='/404' />
            }
        }
    }
}